home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / dflat2.zip / FILEOPEN.C < prev    next >
Text File  |  1991-04-12  |  5KB  |  228 lines

  1. /* ----------- fileopen.c ------------- */
  2.  
  3. #include <string.h>
  4. #ifdef MSC
  5. #include <direct.h>
  6. #else
  7. #include <dir.h>
  8. #endif
  9. #include <dos.h>
  10. #include <ctype.h>
  11. #include "dflat.h"
  12.  
  13. static int DlgFileOpen(char *, char *, DBOX *);
  14. static int DlgFnOpen(WINDOW, MESSAGE, PARAM, PARAM);
  15. static void InitDlgBox(WINDOW);
  16. static void StripPath(char *);
  17. static int IncompleteFilename(char *);
  18.  
  19. static char OrigSpec[80];
  20. static char FileSpec[80];
  21. char FileName[80];
  22.  
  23. static int Saving;
  24. extern DBOX FileOpen;
  25. extern DBOX SaveAs;
  26.  
  27. /*
  28.  * Dialog Box to select a file to open
  29.  */
  30. int DlgOpenFile(char *Fpath, char *Fname)
  31. {
  32.     return DlgFileOpen(Fpath, Fname, &FileOpen);
  33. }
  34.  
  35. /*
  36.  * Dialog Box to select a file to save as
  37.  */
  38. int DlgSaveAs(char *Fname)
  39. {
  40.     return DlgFileOpen(NULL, Fname, &SaveAs);
  41. }
  42.  
  43. /* --------- generic file open ---------- */
  44. static int DlgFileOpen(char *Fpath, char *Fname, DBOX *db)
  45. {
  46.     int  rtn;
  47.     char savedir[80];
  48.  
  49.     getcwd(savedir, sizeof savedir);
  50.     if (Fpath != NULL)    {
  51.         strncpy(FileSpec, Fpath, sizeof(FileSpec)-1);
  52.         Saving = FALSE;
  53.     }
  54.     else    {
  55.         *FileSpec = '\0';
  56.         Saving = TRUE;
  57.     }
  58.     strcpy(FileName, FileSpec);
  59.     strcpy(OrigSpec, FileSpec);
  60.  
  61.     if ((rtn = DialogBox(NULLWND, db, DlgFnOpen)) != FALSE)
  62.         strcpy(Fname, FileName);
  63.     else
  64.         *Fname = '\0';
  65.  
  66.     setdisk(toupper(*savedir) - 'A');
  67.     chdir(savedir);
  68.  
  69.     return rtn;
  70. }
  71.  
  72. /*
  73.  *  Process dialog box messages
  74.  */
  75. static int DlgFnOpen(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  76. {
  77.     switch (msg)    {
  78.         case INITIATE_DIALOG:
  79.             InitDlgBox(wnd);
  80.             break;
  81.         case COMMAND:
  82.             switch ((int) p1)    {
  83.                 case ID_FILENAME:
  84.                     if (p2 != ENTERFOCUS)    {
  85.                         /* allow user to modify the file spec */
  86.                         GetItemText(wnd, ID_FILENAME,
  87.                                 FileName, 65);
  88.                         if (IncompleteFilename(FileName) || Saving)    {
  89.                             strcpy(OrigSpec, FileName);
  90.                             StripPath(OrigSpec);
  91.                         }
  92.                         if (p2 != LEAVEFOCUS)
  93.                             SendMessage(wnd, COMMAND, ID_OK, 0);
  94.                     }
  95.                     return TRUE;
  96.                 case ID_OK:
  97.                     GetItemText(wnd, ID_FILENAME,
  98.                             FileName, 65);
  99.                     strcpy(FileSpec, FileName);
  100.                     if (IncompleteFilename(FileName))    {
  101.                         /* no file name yet */
  102.                         InitDlgBox(wnd);
  103.                         PutItemText(wnd, ID_FILENAME, FileSpec);
  104.                         strcpy(OrigSpec, FileSpec);
  105.                         return TRUE;
  106.                     }
  107.                     else    {
  108.                         GetItemText(wnd, ID_PATH, FileName, 65);
  109.                         strcat(FileName, FileSpec);
  110.                     }
  111.                     break;
  112.                 case ID_FILES:
  113.                     switch ((int) p2)    {
  114.                         case LB_SELECTION:
  115.                             /* selected a different filename */
  116.                             GetDlgListText(wnd, FileName,
  117.                                         ID_FILES);
  118.                             PutItemText(wnd, ID_FILENAME,
  119.                                             FileName);
  120.                             break;
  121.                         case LB_CHOOSE:
  122.                             /* chose a file name */
  123.                             GetDlgListText(wnd, FileName,
  124.                                     ID_FILES);
  125.                             SendMessage(wnd, COMMAND, ID_OK, 0);
  126.                             break;
  127.                         default:
  128.                             break;
  129.                     }
  130.                     return TRUE;
  131.                 case ID_DRIVE:
  132.                     switch ((int) p2)    {
  133.                         case ENTERFOCUS:
  134.                             if (Saving)
  135.                                 *FileSpec = '\0';
  136.                             break;
  137.                         case LEAVEFOCUS:
  138.                             if (Saving)
  139.                                 strcpy(FileSpec, FileName);
  140.                             break;
  141.                         case LB_SELECTION:    {
  142.                             char dd[25];
  143.                             /* selected different drive/dir */
  144.                             GetDlgListText(wnd, dd,
  145.                                                 ID_DRIVE);
  146.                             if (*(dd+2) == ':')
  147.                                 *(dd+3) = '\0';
  148.                             else
  149.                                 *(dd+strlen(dd)-1) = '\0';
  150.                             strcpy(FileName, dd+1);
  151.                             if (*(dd+2) != ':' && *OrigSpec != '\\')
  152.                                 strcat(FileName, "\\");
  153.                             strcat(FileName, OrigSpec);
  154.                             if (*(FileName+1) != ':' && *FileName != '.')    {
  155.                                 GetItemText(wnd, ID_PATH, FileSpec, 65);
  156.                                 strcat(FileSpec, FileName);
  157.                             }
  158.                             else 
  159.                                 strcpy(FileSpec, FileName);
  160.                             break;
  161.                         }
  162.                         case LB_CHOOSE:
  163.                             /* chose drive/dir */
  164.                             InitDlgBox(wnd);
  165.                             break;
  166.                         default:
  167.                             break;
  168.                     }
  169.                     PutItemText(wnd, ID_FILENAME, FileSpec);
  170.                     return TRUE;
  171.  
  172.  
  173.                 default:
  174.                     break;
  175.             }
  176.         default:
  177.             break;
  178.     }
  179.     return DefaultWndProc(wnd, msg, p1, p2);
  180. }
  181.  
  182. /*
  183.  *  Initialize the dialog box
  184.  */
  185. static void InitDlgBox(WINDOW wnd)
  186. {
  187.     if (*FileSpec)
  188.         PutItemText(wnd, ID_FILENAME, FileSpec);
  189.     if (DlgDirList(wnd, FileSpec, ID_FILES, ID_PATH, 0))    {
  190.         StripPath(FileSpec);
  191.         DlgDirList(wnd, "*.*", ID_DRIVE, 0, 0xc010);
  192.     }
  193. }
  194.  
  195. /*
  196.  * Strip the drive and path information from a file spec
  197.  */
  198. static void StripPath(char *filespec)
  199. {
  200.     char *cp, *cp1;
  201.  
  202.     cp = strchr(filespec, ':');
  203.     if (cp != NULL)
  204.         cp++;
  205.     else
  206.         cp = filespec;
  207.     while (TRUE)    {
  208.         cp1 = strchr(cp, '\\');
  209.         if (cp1 == NULL)
  210.             break;
  211.         cp = cp1+1;
  212.     }
  213.     strcpy(filespec, cp);
  214. }
  215.  
  216.  
  217. static int IncompleteFilename(char *s)
  218. {
  219.     int lc = strlen(s)-1;
  220.     if (strchr(s, '?') || strchr(s, '*') || !*s)
  221.         return TRUE;
  222.     if (*(s+lc) == ':' || *(s+lc) == '\\')
  223.         return TRUE;
  224.     return FALSE;
  225. }
  226.  
  227.  
  228.